package com.dantfb.DAO;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;

import org.neo4j.shell.util.json.JSONArray;
import org.neo4j.shell.util.json.JSONException;
import org.neo4j.shell.util.json.JSONObject;

import com.dantfb.post.Post;
import com.dantfb.util.RESTUtil;

/**
 * The Class PostsDAO.
 */
public class PostsDAO {

	/** The posts dao. */
	private static PostsDAO postsDAO;

	/**
	 * Gets the 10 last post
	 * 
	 * @param post
	 *            the post
	 * @return the post
	 * @throws NumberFormatException 
	 * @throws JSONException
	 */
	// renvoie la liste de de publication dans Neo4J
	public ArrayList<Post> getPosts(String ID,boolean notreMur,boolean postDami) throws NumberFormatException, JSONException {
		JSONArray jsonArray = null;
		ArrayList<Post> posts = new ArrayList<Post>();
		//String query ="MATCH (a)-[r:PUBLISH]->(p) where  a.UUID= '"+ID+"' match  (a2)-[r2:friends]->()-[r3:PUBLISH]->(p2)  return p2,p";
		String query = "MATCH (a)-[r:PUBLISH]-(p) where  a.UUID='"+ID+"' return p;";
		if(postDami && notreMur){
			query = "match  (a2)-[r2:friends]-()-[r3:PUBLISH]-(p2) where  a2.UUID='"+ID+"' return p2";
		}
	    jsonArray = RESTUtil.getData(null, query).getJSONArray("data");
		JSONArray array = null;
		for (int i = 0; i < jsonArray.length(); i++) {
				array = jsonArray.getJSONArray(i);
				System.out.println("array.length() "+array.length());
			for (int j = 0; j < array.length(); j++) { 
				JSONObject item = array.getJSONObject(j);
				JSONObject data = new JSONObject(item.getString("data"));
				Post post = new Post(data.get("UUID").toString(), data.get(
							"date").toString(), data.get("author").toString(), data
							.get("postContent").toString(), data.get("picture")
							.toString(), data.get("link").toString(), Integer.parseInt( (data.get("like").toString())) , null);
				posts.add(post);
			} 
		}
		return posts; 
	}
	
	// renvoie le nom depuis
	/**
	 * Gets the from.
	 * 
	 * @param post
	 *            the post
	 * @return the from
	 */
	/*
	 * public Author getFrom(String ID) { HashMap<String, String> params = new
	 * HashMap<String, String>(); String query = " MATCH (n { author: '" + ID +
	 * "' }) RETURN n "; RESTUtil.getData(params, query); return null; }
	 */

	// renvoie la date de publication par l'id
	/**
	 * Gets the date.
	 * 
	 * @param post
	 *            the post
	 * @return the date
	 */

	/*
	 * public Date getDate(String ID) { HashMap<String, String> params = new
	 * HashMap<String, String>(); String query = " MATCH (n { date: '" + ID +
	 * "' }) RETURN n "; RESTUtil.getData(params, query); return null; }
	 */
	// insere dans neo4j
	/**
	 * Adds the post.
	 * 
	 * @param post
	 *            the post
	 */
	public void addPost(Post post) {
		HashMap<String, String> postMap = new HashMap<String, String>();
		postMap.put("UUID", post.getPostID());
		postMap.put("date", post.getDate());
		postMap.put("author", post.getAuthor());// id + name
		postMap.put("postContent", post.getContent());
		postMap.put("picture", post.getPicture());// lien vers picture si existe
		postMap.put("link", post.getLink());// lien vers la publication
		postMap.put("like", "" + post.getLike());// commment or like
		postMap.put("privacy", "" + post.getPrivacy());// public or private or
														// only
		URI postNode = RESTUtil.createNode("publication");
		RESTUtil.addProperties(postNode, postMap);
		String query = "MATCH (a), (b) WHERE (a.UUID) = '"+post.getAuthor()+"' AND (b.author) = '"+post.getAuthor()+"' CREATE (a)-[r:PUBLISH]->(b) RETURN r;";
		RESTUtil.getData(null, query);
	}

	// supprime dans neo4j
	/**
	 * Delete dao.
	 * 
	 * @param id
	 *            the id
	 */
	public boolean deleteDAO(String id) {
		HashMap<String, String> params = new HashMap<String, String>();
		String query = " MATCH (n { postId: '" + id + "' }) DELETE n ";
		RESTUtil.getData(params, query);
		return true;
	}

	public void incrementLike(String postID, String nbLike) {
		String query = "Match (n) where n.postId='" + postID
				+ "' set n.like='"+nbLike+"' return n;";
		RESTUtil.getData(null, query);
	}

	/**
	 * Gets the single instance of PostsDAO.
	 * 
	 * @return single instance of PostsDAO
	 */
	public static PostsDAO getInstance() {
		if (postsDAO == null) {
			postsDAO = new PostsDAO();
		}
		return postsDAO;
	}
}
